package com.peanut.baby.mvp.expert.live;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.http.ApiException;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.baby.mvp.liveroom.LiveRoomActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.main.expert.live.LiveRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/7/12.
 */

public class ExpertLiveListFragment extends BaseMvpFragment implements PullRecyclerView.OnRecyclerRefreshListener,
        ExpertLiveListContract.View,LiveRecyclerAdapter.OnItemViewClickListener{

    private static final String TAG = ExpertLiveListFragment.class.getSimpleName();
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecyclerView;

    Unbinder unbinder;

    private int pageNo  = 1;
    private int pageCount = 5;

    private ExpertLiveListPresenter presenter;

    private LiveRecyclerAdapter adapter;
    private List<LiveRoom>  rooms;


    public String userId;
    public static ExpertLiveListFragment newInstance(String userId) {
        ExpertLiveListFragment fg = new ExpertLiveListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userId",userId);
        fg.setArguments(bundle);
        return fg;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_expert_live2, null);
        unbinder = ButterKnife.bind(this, rootView);

        pullRecyclerView.setOnRecyclerRefreshListener(this);
        return rootView;
    }

    @Override
    public void initData() {
        userId = getArguments().getString("userId");
        presenter = new ExpertLiveListPresenter(this,getActivity());
        rooms = new ArrayList<>();
        adapter = new LiveRecyclerAdapter(getActivity(),rooms);
        adapter.setOnClickListener(this);
        pullRecyclerView.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecyclerView.setAdapter(adapter);

        pullRecyclerView.postRefreshing();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onLiveListGet(final List<LiveRoom> _rooms) {
        if(_rooms != null && !_rooms.isEmpty()){
            if(pageNo == 1){
                rooms.clear();
            }
            rooms.addAll(_rooms);
            adapter.notifyDataSetChanged();
            pullRecyclerView.enableLoadMore(_rooms.size() >= pageCount); // 当剩余还有大于0页的数据时，开启上拉加载更多
        }
        pullRecyclerView.stopRefresh();
        pullRecyclerView.stopLoadMore();
    }

    @Override
    public void onGetLiveListFailed(int pageNo, String error) {
        showToast(error);
        pullRecyclerView.stopRefresh();
        pullRecyclerView.stopLoadMore();
    }

    @Override
    public void onJoinSuccess(LiveRoom room) {
        dismissProgress();
        room.isEnroled = 1;
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onJoinFailed(String code,String reason) {
        dismissProgress();
        if(!ApiException.isApiException(code)){
            showToast("报名失败 "+reason);
        }

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getLiveList(userId,pageNo,pageCount);
    }

    @Override
    public void onLoadMore() {
        pageNo += 1;
        presenter.getLiveList(userId,pageNo,pageCount);
    }

    @Override
    public void onViewItemClicked(View v, LiveRoom room) {
        switch (v.getId()){
            case R.id.live_container:
                LiveDetailActivity.start(getActivity(),room.roomId);
                break;
            case R.id.live_join:
                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(getActivity());
                    return;
                }

                if(InitManager.getInstance().getUser() == null){
                    LoginActivity.startForResult(getActivity());
                    return;
                }
//                //3.1 added 如果不考虑报名状态 注释删掉
//                if(room.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
//                    LiveRoomActivity.start(getActivity(),room);
//                    return;
//                }

                if(InitManager.getInstance().getUserId().equals(room.expertId)
                        || InitManager.getInstance().getUserId().equals(room.serverId)){
                    LiveDetailActivity.start(getActivity(),room.roomId);
                }else{
                    if(room.isEnroled == 0){
                        showProgress("正在报名...",false);
                        presenter.joinRoom(room);
                    }else{
                        LiveRoomActivity.start(getActivity(),room);
                    }
                }

                break;
        }
    }
}
